//
//  ViewController.m
//  百度地图
//
//  Created by dllo on 15/9/23.
//  Copyright (c) 2015年 孟令学. All rights reserved.
//

#import "ViewController.h"
#import <BaiduMapAPI/BMapKit.h>

@interface ViewController ()<BMKMapViewDelegate, BMKLocationServiceDelegate, BMKPoiSearchDelegate>

@property (nonatomic, strong) BMKMapView *mapview;

@property (nonatomic, strong) BMKLocationService *local;

@property (nonatomic, strong) BMKPointAnnotation *point;

@property (nonatomic, strong) BMKNearbySearchOption *option;

@property (nonatomic, strong) BMKPoiSearch *search;
@property (nonatomic, strong) BMKPoiSearch *poiSearch;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.mapview = [[BMKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:_mapview];
    
    _mapview.mapType = BMKMapTypeSatellite; // 卫星地图
    
    // 实时路况
    [_mapview setTrafficEnabled:YES];
    // 热力图层
    [_mapview setBaiduHeatMapEnabled:YES];
    
    // 设定是否显示定位图层
    [_mapview showsUserLocation];
    
    self.local = [[BMKLocationService alloc] init];
    
    self.local.delegate = self;
    
    _mapview.delegate = self;
    
    self.search = [[BMKPoiSearch alloc]init];
    _search.delegate = self;
    
    
    [self.local startUserLocationService];
    
}

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    if (self.point == nil) {
        self.point = [[BMKPointAnnotation alloc] init];
        _point.coordinate = userLocation.location.coordinate;
        _point.title = @"当前位置";
        [_mapview addAnnotation:_point];
        
        CLLocationCoordinate2D coor;
        // 当前用户经纬度
        coor.latitude = userLocation.location.coordinate.latitude;
        coor.longitude = userLocation.location.coordinate.longitude;
        // 经纬度范围
        BMKCoordinateSpan span;
        span.latitudeDelta = 0.005;
        span.longitudeDelta = 0.005;
        BMKCoordinateRegion region;
        
        // 定位中心及范围
        region.center = coor;
        region.span = span;
        [self.mapview setRegion:region animated:YES];
        self.point.coordinate = coor;
        [self.mapview addAnnotation:_point];
        
        //定义到用户位置
        [_mapview updateLocationData:userLocation];
        //周边检索参数信息类
        self.option = [[BMKNearbySearchOption alloc]init];
        _option.radius = 500;
        _option.pageCapacity = 10;
        _option.location = coor;
        _option.keyword = @"美食";
        BOOL flag = [self.search poiSearchNearBy:_option];
        if (flag) {
            NSLog(@"周边搜索发送成功");
        }else{
            NSLog(@"周边搜索发送失败");
        }
    }
}


- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult *)poiResult errorCode:(BMKSearchErrorCode)errorCode
{
    if (errorCode == BMK_SEARCH_PERMISSION_UNFINISHED ||  errorCode == BMK_SEARCH_RESULT_NOT_FOUND) {
        NSLog(@"鉴权失败, 重新检索");
        [_search poiSearchNearBy:_option];
    }
    if (errorCode == BMK_SEARCH_NO_ERROR) {
        for (BMKPoiInfo *info in poiResult.poiInfoList) {
            self.poiSearch = [[BMKPoiSearch alloc]init];
            _poiSearch.delegate = self;
            BMKPoiDetailSearchOption *opi = [[BMKPoiDetailSearchOption alloc]init];
            opi.poiUid = info.uid;
            
            BOOL flag = [self.poiSearch poiDetailSearch:opi];
            if (flag) {
                NSLog(@"成功");
            }else{
                NSLog(@"失败");
            }
         }
    }
}

- (void)onGetPoiDetailResult:(BMKPoiSearch *)searcher result:(BMKPoiDetailResult *)poiDetailResult errorCode:(BMKSearchErrorCode)errorCode{
    if (errorCode == BMK_SEARCH_NO_ERROR) {
        NSLog(@"%@  ----------   %@ --------------- %@ ---------------- %@", poiDetailResult.name, poiDetailResult.address, poiDetailResult.detailUrl, poiDetailResult.tag);
        BMKPointAnnotation *point = [[BMKPointAnnotation alloc] init];
        point.coordinate = poiDetailResult.pt;
        point.title = poiDetailResult.name;
        [_mapview addAnnotation:point];
    }
}

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        if (annotation  != _point) {
            BMKAnnotationView *view = [[BMKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"anno"];
            [view setImage:[UIImage imageNamed:@"pin_green@2x.png"]];
            view.annotation = annotation;
            return view;
        }
    }
    return nil;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
